package org.bsc.mgwt.client.mvp;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.place.shared.PlaceTokenizer;

public class DynamicPlaceHistoryMapper implements PlaceHistoryMapper {

	public java.util.Map<String,Place> tokenizerMap = new java.util.HashMap<String,Place>();
	
	final PlaceTokenizer<Place> tokenizer = new PlaceTokenizer<Place>() {

		@Override
		public Place getPlace(String token) {
			return null;
		}

		@Override
		public String getToken(Place place) {
			String className = place.getClass().getName();
			int index = className.lastIndexOf('.');
			className = className.substring(++index, className.length());
			if( className.endsWith("Place")) {
				className = className.substring(0, className.length()-5);
			}
			return className.toLowerCase();
		}
	
	};
	

	public void add( Place place ) {
		tokenizerMap.put( tokenizer.getToken(place), place  );
	}
	
	@Override
	public Place getPlace(String token) {
		return tokenizerMap.get(token);
	}

	@Override
	public String getToken(Place place) {
		return tokenizer.getToken(place);
	}

}
